# coding=utf8
from django.shortcuts import render_to_response
from django.http import Http404
from django.template.context import RequestContext
from django.core.paginator import Paginator
from django.db.models import Q
from tussin.news.models import Article, Category
from tussin.ads.models import AdBlock, AdItem

def latest(request):
    articles = Article.objects.filter(category=1)
    return render_to_response('latest.html', {"articles": articles})

def article_list(request, channel_id):
    try:
        channel = Category.objects.get(pk=channel_id)
    except Category.DoesNotExist:
        raise Http404
    observe_articles = Article.objects.filter(category__name="观察", status=2)[0:10]
    trends_articles = Article.objects.filter(category__name="趋势", status=2)[0:10]
    
    children = Category.objects.filter(parent__id=channel_id)
    if (channel.parent is not None) or (children.count() == 0):
        # ads on page
        ads = AdItem.objects.filter(ad_block__page__exact='subchannel_page')
        top_ad = ads.filter(ad_block__name="二级频道顶部通栏广告")[:1]
        title_ad = ads.filter(ad_block__name="二级频道标题栏广告")[:1]
        right_ad = ads.filter(ad_block__name="二级频道右侧展示广告")[:1]
        medium_ad = ads.filter(ad_block__name="二级频道中部通栏广告")[:1]
        left_ad = ads.filter(ad_block__name="二级频道左侧图列广告")[:4]
        # end ads
        image_headlines = Article.objects.filter(category=channel, is_headline=True, status=2)[0:3]
        #articles = Article.objects.filter(category=channel, is_headline=False, status=2)
        articles = Article.objects.filter(category=channel, status=2)
        pager = Paginator(articles, 15)
        page_num = int(request.GET.get('p', 1))
        if page_num < 0 or page_num > pager.num_pages:
            page_num = 1
        try:
            cur_page = pager.page(page_num)
        except EmptyPage:
            cur_list = pager.page(1)
        return render_to_response('news/list.html', {'channel': channel, 'ads': {'top_ad1': top_ad, 'title_ad': title_ad, 'sidebar_ad': right_ad, 'medium_ad': medium_ad, 'left_ad': left_ad}, 'observation': observe_articles, 'trends': trends_articles, 'headlines': image_headlines, 'cur_page': cur_page, 'articles': cur_page.object_list, 'page_num': page_num, 'page_range': pager.page_range}, context_instance=RequestContext(request))
    else:
        # ads on page
        ads = AdItem.objects.filter(ad_block__page__exact='channel_page')
        top_ad = ads.filter(ad_block__name="一级频道顶部通栏广告")[:1]
        title_ad = ads.filter(ad_block__name="一级频道标题栏广告")[:1]
        right_ad = ads.filter(ad_block__name="一级频道右侧展示广告")
        medium_ad = ads.filter(ad_block__name="一级频道中部通栏广告")[:1]
        # end ads
        headlines = {}
        articles = []
        for child in children:
            headline = Article.objects.filter(category=child,status=2,is_headline=True)[:1]
            if headline.count() > 0:
                articles.append({'channel': child, 'headline': headline[0], 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:10]})
                #articles[child] = {'headline': headline[0], 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:12]}
            else:
                articles.append({'channel': child, 'headline': None, 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:10]})
                #articles[child] = {'headline': None, 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:12]}
        return render_to_response('news/channel1.html', {'channel': channel, 'subchannel': children, 'ads': {'top_ad1': top_ad, 'title_ad': title_ad, 'sidebar_ad': right_ad, 'medium_ad': medium_ad}, 'observation': observe_articles, 'trends': trends_articles,'headlines':headlines, 'allarticles': articles }, context_instance=RequestContext(request))

def article_detail(request, article_id):
    try:
        article = Article.objects.get(pk=article_id)
    except Article.DoesNotExist:
        raise Http404
    ads = AdItem.objects.filter(ad_block__page__exact='detail_page')
    top_ad = ads.filter(ad_block__name="文章页面顶部通栏广告")[:1]
    sidebar_ad = ads.filter(ad_block__name="文章页面右侧展示广告")[:1]
    observe_articles = Article.objects.filter(category__name="观察", status=2)[0:10]
    trends_articles = Article.objects.filter(category__name="趋势", status=2)[0:10]
    #observe_articles = Article.objects.filter(category, status=2)[0:10]
    #trends_articles = Article.objects.filter(category=15, status=2)[0:10]
    return render_to_response('news/detail.html', {'article': article, 'ads': {'top_ad1': top_ad, 'sidebar_ad': sidebar_ad}, 'observation': observe_articles, 'trends': trends_articles }, context_instance=RequestContext(request))

def search(request):
    error = False
    query = request.GET.get('q', '')
    if query is not '':
        articles = Article.objects.filter(Q(title__contains=query)|Q(content__contains=query))
    else:
        error = True
        articles = None
    return render_to_response('news/search.html', {'articles': articles, 'query': query, 'error': error}, context_instance=RequestContext(request))
